<template>
    <div class="box-wrapper">
        <div class="search-wrap">
            <div class="item">
                <el-input v-model="name" placeholder="类型名称"></el-input>
            </div>
            <div class="item">
                <el-button class="btn bg-blue" @click="queryClick">查询</el-button>
                <el-button class="btn" @click="resetClick">重置</el-button>
            </div>
        </div>
        <div class="query-box t-rig" v-if="hasPermission('labAccidentTypeAdd')">
            <el-button @click="addClick" class="btn" type="primary">新增</el-button>
        </div>
        <el-table v-loading="loading" ref="multipleTable" :header-cell-style="headerStyle" :data="tableData" tooltip-effect="dark" style="width: 100%">
            <el-table-column prop="sort" label="序号">
            </el-table-column>
            <el-table-column prop="name" label="名称">
            </el-table-column>
            <el-table-column prop="oprate" label="操作">
                <template slot-scope="scope">
                    <el-button v-if="hasPermission('labAccidentTypeUpdate')" @click="handleClick(scope.row,'modify')" type="text" size="small">修改</el-button>
                    <el-button @click="handleClick(scope.row,'detail')" type="text" size="small">查看</el-button>
                    <el-button v-if="hasPermission('labAccidentTypeDelete')" @click="delClick(scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="page" v-if="total > 0 && tableData.length>0">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="currentPage"
                :page-sizes="[10,20,30,40]"
                :page-size="pageSize"
                layout="prev,pager,next,sizes,total"
                :total="total">
            </el-pagination>
        </div>
        <addAccident :detailUserId="detailUserId" :dataTitle="dataTitle" :isDialog="isDialog" :dataType="dataType" :dataInfo="dataInfo" @dialogChange="dialogChange" />
    </div>
</template>
<script>
import {request} from "@/assets/js/utils/request";
import addAccident from './addAccident'
import mixin from '@/assets/js/mixin'
export default{
    mixins: [mixin],
    components: {
        addAccident
    },
    data() {
        return {
            loading: false,
            tableData: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            isDialog: false,
            headerStyle: {
                background: '#f2f2f2',
                color: '#000',
                // textAlign: 'center'
            },
            dataType: '',
            dataInfo: {},
            dataTitle: '',
            name: '',
            userCode: '',
            jaccountCode: '',
            companyName: '',
            detailUserId: ''
        }
    },
    created() {
        this.initData()
    },
    methods: {
        getDetail(params) {
            request({
                url: '/manager/system/user/userInfo.jsp',
                method: 'post',
                data: {
                    userId: params,
                }
            }).then(res => {
                if(res.code == 200) {
                    this.dataInfo = res.data
                }
            })
        },
        delClick(params) {
            this.$confirm('确认删除这条数据?','提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                request({
                    url: '/manager/configmag/accidentSave.jsp',
                    method: 'post',
                    data: {
                        txtAction: 'delete',
                        id: params.id
                    }
                }).then(res => {
                    if(res.code == 200) {
                        this.$message({
                            message: '已删除',
                            type: 'success'
                        });
                        if (this.tableData.length === 1) {
                            this.currentPage--
                            if (this.currentPage <= 0) {
                                this.currentPage = 1
                            }
                        }
                        this.initData()
                    }
                })
            }).catch(() => {
                
            });
        },
        handleClick(params,type) {
            this.detailUserId = params.userId
            this.isDialog = true
            if(type == 'modify') {
                this.dataTitle = '修改'
            }
            if(type == 'detail') {
                this.dataTitle = '详情'
            }
            this.dataType = type
            this.dataInfo = params
        },
        dialogChange() {
            this.isDialog = false
            this.initData()
        },
        addClick() {
            this.dataInfo = {}
            this.dataType = ''
            this.dataTitle = '新增'
            this.isDialog = true
        },
        queryClick() {
            this.initData()
        },
        resetClick() {
            this.name = ''
            this.currentPage = 1
            this.initData()
        },
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.initData()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.initData()
        },
        initData() {
            this.loading = true
            request({
                url: '/manager/configmag/accidentQuery.jsp',
                method: 'post',
                data: {
                    txtAction: 'query',
                    name: this.name,
                    page: this.currentPage,
                    limit: this.pageSize
                }
            }).then(res => {
                if(res.code == 200) {
                    this.tableData = res.data || []
                    this.total = res.count
                    this.loading = false
                }
            })
        }
    }
}
</script>
<style lang="less" scoped>
.box-wrapper{
    background: #fff;
    .title-name{
        line-height: 30px;
        border-bottom: 1px solid #ddd;
        margin-bottom: 10px;
        span{
            color: #409EFF;
            display: inline-block;
            border-bottom: 2px solid #409EFF;
        }
    }
}
/deep/.search-wrap{
    margin: 20px 0;
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    background: #b1cbff;
    .item{
        width: 25%;
        padding: 5px;
    }
    .btn{
        padding: 0;
        width: 80px;
        height: 32px;
        border: none;
        // background: #016aed;
    }
    .bg-blue{
        color: #ffffff;
        background: #016aed;
    }
    .el- {
        width: 100%;
    }
    .el-input{
        .el-input__inner{
            height: 32px;
        }
    }
}
.query-box{
    margin: 20px 0;
    .btn{
        padding: 0;
        width: 100px;
        height: 32px;
        background: #016aed;
    }
}
.tabHeader{
    background: #f2f2f2;
}
.page{
    padding: 20px 0;
    border-top: 1px solid #d5d5d5;
    text-align: right;
}
</style>